package org.example.Dao;

import org.example.Entity.Product;
import org.example.util.jdbcUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 实现了ProductDao接口的具体类，用于对产品信息进行数据库操作。
 */
public class ProductDaoImp implements ProductDao {

    @Override
    public void addProduct(Product product) {
        try (Connection connection = jdbcUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement("INSERT INTO products VALUES (?, ?, ?, ?)");) {
            // 设置SQL语句参数
            statement.setString(1, product.getId());
            statement.setString(2, product.getName());
            statement.setDouble(3, product.getPrice());
            statement.setInt(4, product.getStockQuantity());
            // 执行SQL语句
            statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace(); // 处理异常
        }
    }

    @Override
    public List<Product> getAllProducts() {
        ArrayList<Product> products = new ArrayList<>();
        try (Connection connection = jdbcUtil.getConnection();
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery("SELECT * FROM products");) {
            // 处理结果集
            while (resultSet.next()) {
                String id = resultSet.getString("id");
                String name = resultSet.getString("name");
                double price = resultSet.getDouble("price");
                int stockQuantity = resultSet.getInt("stock_quantity");

                Product product = new Product(id, name, price, stockQuantity);
                products.add(product);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return products;
    }

    @Override
    public Product getProductById(String id) {
        try (Connection connection = jdbcUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement("SELECT * FROM products WHERE id = ?")) {

            statement.setString(1, id);

            try (ResultSet resultSet = statement.executeQuery()) {
                // 处理结果集
                if (resultSet.next()) {
                    String name = resultSet.getString("name");
                    double price = resultSet.getDouble("price");
                    int stockQuantity = resultSet.getInt("stock_quantity");

                    return new Product(id, name, price, stockQuantity);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace(); // 处理异常
        }

        return null;
    }

    @Override
    public Product getProductByName(String name) {
        try (Connection connection = jdbcUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement("SELECT * FROM products WHERE name = ?")) {

            statement.setString(1, name);

            try (ResultSet resultSet = statement.executeQuery()) {
                // 处理结果集
                if (resultSet.next()) {
                    String id = resultSet.getString("id");
                    double price = resultSet.getDouble("price");
                    int stockQuantity = resultSet.getInt("stock_quantity");

                    return new Product(id, name, price, stockQuantity);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace(); // 处理异常
        }

        return null;
    }

    @Override
    public List<Product> getProductsByPriceRange(double minPrice, double maxPrice) {
        List<Product> products = new ArrayList<>();

        try (Connection connection = jdbcUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement("SELECT * FROM products WHERE price BETWEEN ? AND ?")) {

            // 设置SQL语句参数
            statement.setDouble(1, minPrice);
            statement.setDouble(2, maxPrice);

            try (ResultSet resultSet = statement.executeQuery()) {
                // 处理结果集
                while (resultSet.next()) {
                    String id = resultSet.getString("id");
                    String name = resultSet.getString("name");
                    double price = resultSet.getDouble("price");
                    int stockQuantity = resultSet.getInt("stock_quantity");

                    Product product = new Product(id, name, price, stockQuantity);
                    products.add(product);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace(); // 处理异常
        }

        return products;
    }

    @Override
    public void deleteProductById(String id) {
        try (Connection connection = jdbcUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement("DELETE FROM products WHERE id = ?")) {

            // 设置SQL语句参数
            statement.setString(1, id);
            // 执行SQL语句
            statement.executeUpdate();

            System.out.println("Delete " +  id + " successfully!");
        } catch (SQLException e) {
            e.printStackTrace(); // 处理异常
        }
    }

    @Override
    public void updateProductName(String id, String newName) {
        try (Connection connection = jdbcUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement("UPDATE products SET name = ? WHERE id = ?")) {

            // 设置SQL语句参数
            statement.setString(1, newName);
            statement.setString(2, id);
            // 执行SQL语句
            statement.executeUpdate();

            System.out.println("Update " +  id + " successfully!");
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateProductPrice(String id, double newPrice) {
        try (Connection connection = jdbcUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement("UPDATE products SET price = ? WHERE id = ?")) {

            // 设置SQL语句参数
            statement.setDouble(1, newPrice);
            statement.setString(2, id);
            // 执行SQL语句
            statement.executeUpdate();

            System.out.println("Update " +  id + " successfully!");
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateProductStockQuantity(String id, int newStockQuantity) {
        try (Connection connection = jdbcUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement("UPDATE products SET stock_quantity = ? WHERE id = ?")) {
            // 设置SQL语句参数
            statement.setInt(1, newStockQuantity);
            statement.setString(2, id);
            // 执行SQL语句
            statement.executeUpdate();

            System.out.println("Update " +  id + " successfully!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
